package org.jeecg.modules.demo.hydrology.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.demo.hydrology.dto.CgkcdssDto;
import org.jeecg.modules.demo.hydrology.entity.Cgkcdss;
import org.jeecg.modules.demo.hydrology.mapper.CgkcdssMapper;
import org.jeecg.modules.demo.hydrology.service.ICgkcdssService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysDict;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.mapper.SysDepartMapper;
import org.jeecg.modules.system.mapper.SysDictItemMapper;
import org.jeecg.modules.system.mapper.SysDictMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 长观孔历史
 * @Author: jeecg-boot
 * @Date:   2024-05-11
 * @Version: V1.0
 */
@Service
public class CgkcdssServiceImpl extends ServiceImpl<CgkcdssMapper, Cgkcdss> implements ICgkcdssService {
    @Autowired
    private CgkcdssMapper cgkcdssMapper;
    @Autowired
    private SysDepartMapper sysDepartMapper;
    @Autowired
    private SysDictMapper sysDictMapper;
    @Autowired
    private SysDictItemMapper sysDictItemMapper;
    /**
     * 长观孔实时-分页列表查询
     * @param page
     * @param queryWrapper
     * @return
     */
    @Override
    public IPage<CgkcdssDto> getList(Page<Cgkcdss> page, QueryWrapper<Cgkcdss> queryWrapper) {
        Page<Cgkcdss> cgkcdssPage = cgkcdssMapper.selectPage(page, queryWrapper);
        List<CgkcdssDto> collect = cgkcdssPage.getRecords().stream().map(cgkcdss -> {
            CgkcdssDto cgkcdssDto = new CgkcdssDto();
            BeanUtils.copyProperties(cgkcdss, cgkcdssDto);
            QueryWrapper<SysDepart> sysDepartQueryWrapper = new QueryWrapper<>();
            sysDepartQueryWrapper.eq("org_code", cgkcdss.getOrgCode());
            SysDepart sysDepart = sysDepartMapper.selectOne(sysDepartQueryWrapper);
            if (sysDepart != null){
                cgkcdssDto.setStationName(sysDepart.getDepartName());
            }
            QueryWrapper<SysDict> sysDictQueryWrapper = new QueryWrapper<>();
            sysDictQueryWrapper.eq("dict_code","point_status");
            SysDict sysDict = sysDictMapper.selectOne(sysDictQueryWrapper);
            if (sysDict != null){
                QueryWrapper<SysDictItem> sysDictItemQueryWrapper = new QueryWrapper<>();
                sysDictItemQueryWrapper.eq("dict_id",sysDict.getId()).eq("item_value",cgkcdss.getPointStatus());
                SysDictItem sysDictItem = sysDictItemMapper.selectOne(sysDictItemQueryWrapper);
                if (sysDictItem != null) {
                    cgkcdssDto.setPointStatusItemText(sysDictItem.getItemText());
                }
            }
            QueryWrapper<SysDict> sysDictQueryWrapper1 = new QueryWrapper<>();
            sysDictQueryWrapper1.eq("dict_code","sensor_type");
            SysDict sysDict1 = sysDictMapper.selectOne(sysDictQueryWrapper1);
            if (sysDict1 != null){
                QueryWrapper<SysDictItem> sysDictItemQueryWrapper1 = new QueryWrapper<>();
                sysDictItemQueryWrapper1.eq("dict_id",sysDict1.getId()).eq("item_value",cgkcdss.getSensorType());
                SysDictItem sysDictItem1 = sysDictItemMapper.selectOne(sysDictItemQueryWrapper1);
                if (sysDictItem1 != null) {
                    cgkcdssDto.setSensorTypeItemText(sysDictItem1.getItemText());
                }
            }
            return cgkcdssDto;
        }).collect(Collectors.toList());
        IPage<CgkcdssDto> cgkcdssDtoIPage = new Page<>();
        cgkcdssDtoIPage.setRecords(collect);
        cgkcdssDtoIPage.setTotal(cgkcdssPage.getTotal());
        cgkcdssDtoIPage.setPages(page.getPages());
        cgkcdssDtoIPage.setSize(page.getSize());
        return cgkcdssDtoIPage;
    }
}
